package com.naire.modul.code_inc.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.common.dao.GeneralDaoImpl;
import net.common.service.PageBean;
import net.common.util.BeanUtils;

import com.naire.modul.code_inc.dao.Code_incDao;
import com.naire.modul.code_inc.pojo.Code_inc;
import com.naire.modul.code_inc.vo.Code_incVo;

public class Code_incDaoImpl extends GeneralDaoImpl implements Code_incDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Code_incVo> search(PageBean pageBean) throws Exception {
		StringBuilder hql = new StringBuilder("from Code_inc where flag=0");
		List<Code_incVo> d = new ArrayList<Code_incVo>();
		List<Code_inc> l = hibernateHandler.query(hql, pageBean);
		for(Code_inc bean : l) {
			Code_incVo dv = new Code_incVo();
			BeanUtils.copyProperties(bean, dv);
			d.add(dv);
		}
		return d;
	}

	@Override
	public void insert(Code_incVo bean) throws Exception {
		Code_inc pojo = new Code_inc();
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.insert(pojo);
	}

	@Override
	public void update(Code_incVo bean) throws Exception {
		Code_inc pojo = (Code_inc)hibernateHandler.find(Code_inc.class, bean.getInc_id());
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.update(pojo);
	}

	@Override
	public void updateFlag(Code_incVo bean) throws Exception {
		update(bean);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Code_incVo find(String code_num, String preffix) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "from Code_inc where code_num=:code_num";
		if(preffix != null) {
			hql += " and preffix=:preffix";
			params.put("preffix", preffix);
		} else {
			hql += " and preffix is null";
		}
		params.put("code_num", code_num);
		List<Code_inc> list = hibernateHandler.executeQuery(hql, params);
		if(list != null && list.size() > 0) {
			Code_incVo bean = new Code_incVo();
			BeanUtils.copyProperties(list.get(0), bean);
			return bean;
		}
		return null;
	}
}
